#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:homework8_2.py
# author:wmf
# datetime:2021/5/8 20:40
# software: PyCharm
'''
this is function description
'''
# import module your need

# 2 给定一组数据网址数据，请判断这些网址是否可以访问； 用多线程的方式来实现；
#   请查资料，Python的 requests库，如何判断一个网址可以访问；
from concurrent.futures.thread import ThreadPoolExecutor
import requests


def getHtmlText(url):
    try:  # 网络连接有风险，异常处理很重要
        r = requests.get(url, timeout=30)  # 查一下这个方法的使用
        r.raise_for_status()  # 如果状态不是200，引发HTTPError异常
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return "产生异常"


with open("url_data.txt", 'r', encoding='utf-8') as f:
    info = f.readlines()


def getUrlData(name, counter):
    for x in range(0, 200):
        getHtmlText(info[counter + x])
        print(f"{name}:{info[counter + x]}{getHtmlText(info[counter + x])}")


if __name__ == "__main__":
    with ThreadPoolExecutor(max_workers=5) as t:
        task1 = t.submit(getUrlData, 'task1', 0)
        task2 = t.submit(getUrlData, 'task2', 200)
        task3 = t.submit(getUrlData, 'task3', 400)
        task4 = t.submit(getUrlData, 'task4', 600)
        task5 = t.submit(getUrlData, 'task5', 800)
